/* JS Document */

/*******************************************************************************
 * 
 * [Table of Contents]
 * 
 * 1. Vars and Inits 2. Set Header 3. Init Search 4. Init Menu 5. Init Quantity
 * 
 * 
 ******************************************************************************/

$(document).ready(
		function() {
			"use strict";

			/*
			 * 
			 * 1. Vars and Inits
			 * 
			 */

			var header = $('.header');
			var hambActive = false;
			var menuActive = false;

			setHeader();

			$(window).on('resize', function() {
				setHeader();
			});

			$(document).on('scroll', function() {
				setHeader();
			});

			initSearch();
			initMenu();
			initQuantity();

			/*
			 * 
			 * 2. Set Header
			 * 
			 */

			function setHeader() {
				if ($(window).scrollTop() > 100) {
					header.addClass('scrolled');
				} else {
					header.removeClass('scrolled');
				}
			}

			/*
			 * 
			 * 3. Init Search
			 * 
			 */

			function initSearch() {
				if ($('.search').length && $('.search_panel').length) {
					var search = $('.search');
					var panel = $('.search_panel');

					search.on('click', function() {
						panel.toggleClass('active');
					});
				}
			}

			/*
			 * 
			 * 4. Init Menu
			 * 
			 */

			function initMenu() {
				if ($('.hamburger').length) {
					var hamb = $('.hamburger');

					hamb.on('click', function(event) {
						event.stopPropagation();

						if (!menuActive) {
							openMenu();

							$(document).one('click', function cls(e) {
								if ($(e.target).hasClass('menu_mm')) {
									$(document).one('click', cls);
								} else {
									closeMenu();
								}
							});
						} else {
							$('.menu').removeClass('active');
							menuActive = false;
						}
					});

					// Handle page menu
					if ($('.page_menu_item').length) {
						var items = $('.page_menu_item');
						items.each(function() {
							var item = $(this);

							item.on('click', function(evt) {
								if (item.hasClass('has-children')) {
									evt.preventDefault();
									evt.stopPropagation();
									var subItem = item.find('> ul');
									if (subItem.hasClass('active')) {
										subItem.toggleClass('active');
										TweenMax.to(subItem, 0.3, {
											height : 0
										});
									} else {
										subItem.toggleClass('active');
										TweenMax.set(subItem, {
											height : "auto"
										});
										TweenMax.from(subItem, 0.3, {
											height : 0
										});
									}
								} else {
									evt.stopPropagation();
								}
							});
						});
					}
				}
			}

			function openMenu() {
				var fs = $('.menu');
				fs.addClass('active');
				hambActive = true;
				menuActive = true;
			}

			function closeMenu() {
				var fs = $('.menu');
				fs.removeClass('active');
				hambActive = false;
				menuActive = false;
			}

			/*
			 * 
			 * 5. Init Quantity
			 * 
			 */

			function initQuantity() {
				// Handle product quantity input
				if ($('.product_quantity').length) {
					var input = $('.quantity_input');
					var incButton = $('.quantity_inc_button');
					var decButton = $('.quantity_dec_button');

					var originalVal;
					var endVal;
					incButton.each(function() {

						$(this).on(
								'click',
								function() {
									originalVal = $(this).parents(
											"div.product_quantity").children(
											".quantity_input").val();
									endVal = parseFloat(originalVal) + 1;
									if (endVal <= 1) {
										endVal = 1;
									}
									$(this).parents("div.product_quantity")
											.children(".quantity_input").val(
													endVal);
									// input.val(endVal);
								});
					})

					decButton.each(function() {

						$(this).on(
								'click',
								function() {
									originalVal = $(this).parents(
											"div.product_quantity").children(
											".quantity_input").val();
									endVal = parseFloat(originalVal) - 1;
									if (endVal <= 1) {
										endVal = 1;
									}
									$(this).parents("div.product_quantity")
											.children(".quantity_input").val(
													endVal);
									// input.val(endVal);
								});
					})

					// decButton.on('click', function() {
					// originalVal = input.val();
					// if (originalVal > 0) {
					// endVal = parseFloat(originalVal) - 1;
					// input.val(endVal);
					// }
					// });
				}
			}
			
			//6 全选
			//cksgoods  s_goods
			$("#s_goods").click(function(){
				$(".cksgoods").prop("checked",$(this).prop("checked"));
			});

		});